package com.cyx.netty.gateway.support;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultServerWebExchange implements ServerWebExchange{

    private final Map<String, Object> attributes = new ConcurrentHashMap<>();

    private final FullHttpRequest request;

    private final ChannelHandlerContext channelHandlerContext;

    public DefaultServerWebExchange(FullHttpRequest request,
        ChannelHandlerContext channelHandlerContext) {
        this.request = request;
        this.channelHandlerContext=channelHandlerContext;
    }

    @Override
    public FullHttpRequest getRequest() {
        return this.request;
    }

    @Override
    public ChannelHandlerContext getChannelHandlerContext() {
        return this.channelHandlerContext;
    }


    @Override
    public Map<String, Object> getAttributes() {
        return this.attributes;
    }

    @Override @SuppressWarnings("unchecked")
    public <T> Optional<T> getAttribute(String name) {
        return Optional.ofNullable((T) this.attributes.get(name));
    }
}
